import {
   Body,
   Controller,
   Get,
   HttpException,
   HttpStatus,
   Optional,
   Param,
   Post,
   Query,
   SetMetadata,
   UseFilters,
   UseGuards,
   UseInterceptors,
   UsePipes,
   Headers,
   Ip
} from '@nestjs/common'
import { AppService } from './app.service'
import { TestFilter } from './test.filter'
import { TestGuard } from './test.guard'
import { TestPipe } from './test.pipe'
import { TestInterceptor } from './test.interceptor'

@Controller()
@SetMetadata('roles', ['admin']) // @SetMetadata 指定 metadata, 可以在守卫或者拦截器中获取
export class AppController {
   // @Optional 代表这个依赖是可选的，就算没有也不会报错
   constructor(@Optional() private readonly appService: AppService) {}

   @Get('/user/:type')
   @SetMetadata('roles', ['admin', 'user'])
   //   @UseFilters 应用顾虑器
   @UseFilters(TestFilter)

   // 同样得守卫、拦截器、管道也是这么
   @UseGuards(TestGuard)
   @UsePipes(TestPipe)
   @UseInterceptors(TestInterceptor)

   // 管道更多还是在参数中使用
   getHello(
      @Headers() header: Record<any, any>, // 获取请求头的内容
      @Param('type', TestPipe) type: string,
      @Query('id', TestPipe) id: string,
      @Ip() ip: string // 获取IP地址
   ): string {
      console.log(header)
      console.log(ip)
      console.log({ id, type })
      throw new HttpException('错误', HttpStatus.BAD_REQUEST)
      return this.appService.getHello()
   }

   // 如果使用post请求
   @Post('/user')
   getHello2(@Body() body: any): string {
      console.log(body)
      return this.appService.getHello()
   }

   //  @Get、@Post 外，还可以用 @Put、@Delete、@Patch、@Options、@Head 装饰器分别接受 put、delete、patch、options、head 请求
}
